package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.ProductDao;
import guda.shop.cms.dao.ProductFashionDao;
import guda.shop.cms.entity.*;
import guda.shop.cms.manager.*;
import guda.shop.common.file.FileNameUtils;
import guda.shop.common.hibernate3.Updater;
import guda.shop.common.image.ImageScale;
import guda.shop.common.image.ImageUtils;
import guda.shop.common.page.Pagination;
import guda.shop.common.web.springmvc.RealPathResolver;
import guda.shop.core.entity.Website;
import guda.shop.core.manager.WebsiteMng;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@Service
@Transactional
public class ProductMngImpl
        implements ProductMng {

    @Autowired
    private ProductFashionDao productFashionDao;

    @Autowired
    private ProductFashionMng productFashionMng;

    @Autowired
    private CollectMng collectMng;

    @Autowired
    private ConsultMng consultMng;

    @Autowired
    private CartItemMng cartItemMng;

    @Autowired
    private RealPathResolver realPathResolver;

    @Autowired
    private WebsiteMng websiteMng;

    @Autowired
    private CategoryMng categoryMng;

    @Autowired
    private ProductTagMng productTagMng;

    @Autowired
    private ProductTextMng productTextMng;

    @Autowired
    private ShopConfigMng shopConfigMng;

    @Autowired
    private BrandMng brandMng;

    @Autowired
    private ImageScale imageScale;

    @Autowired
    private ProductExtMng productExtMng;

    @Autowired
    private ProductStandardMng productStandardMng;

    @Autowired
    private ProductDao productDao;

    public List<Product> getList(Long paramLong1, Long paramLong2, String paramString) {
        return this.productDao.getList(paramLong1, paramLong2, paramString, true);
    }

    public void resetSaleTop() {
        List localList = getList(null, null, null);
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext()) {
            Product localProduct = (Product) localIterator.next();
            localProduct.setSaleCount(Integer.valueOf(0));
            update(localProduct);
        }
    }

    public void resetProfitTop() {
        List localList = getList(null, null, null);
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext()) {
            Product localProduct = (Product) localIterator.next();
            localProduct.setLiRun(Double.valueOf(0.0D));
            update(localProduct);
        }
    }

    @Transactional(readOnly = true)
    public Pagination getPage(Long paramLong1, Long paramLong2, String paramString1, String paramString2, Boolean paramBoolean1, Boolean paramBoolean2, Boolean paramBoolean3, Boolean paramBoolean4, Boolean paramBoolean5, Long paramLong3, Double paramDouble1, Double paramDouble2, Integer paramInteger1, Integer paramInteger2, int paramInt1, int paramInt2) {
        Pagination localPagination;
        if (paramLong2 != null)
            localPagination = this.productDao.getPageByCategory(paramLong2, paramString1, paramString2, paramBoolean1, paramBoolean2, paramBoolean3, paramBoolean4, paramBoolean5, paramLong3, paramDouble1, paramDouble2, paramInteger1, paramInteger2, paramInt1, paramInt2, false);
        else
            localPagination = this.productDao.getPageByWebsite(paramLong1, paramString1, paramString2, paramBoolean1, paramBoolean2, paramBoolean3, paramBoolean4, paramBoolean5, paramLong3, paramDouble1, paramDouble2, paramInteger1, paramInteger2, paramInt1, paramInt2, false);
        return localPagination;
    }

    public Pagination getPage(int paramInt1, int paramInt2, int paramInt3) {
        return this.productDao.getPage(paramInt1, paramInt2, paramInt3, true);
    }

    @Transactional(readOnly = true)
    public Pagination getPageForTag(Long paramLong1, Long paramLong2, Long paramLong3, Boolean paramBoolean1, Boolean paramBoolean2, int paramInt1, int paramInt2) {
        Pagination localPagination;
        if (paramLong3 != null)
            localPagination = this.productDao.getPageByTag(paramLong3, paramLong2, paramBoolean1, paramBoolean2, paramInt1, paramInt2, true);
        else if (paramLong2 != null)
            localPagination = this.productDao.getPageByCategory(paramLong2, null, null, Boolean.valueOf(true), paramBoolean1, paramBoolean2, null, null, null, null, null, null, null, paramInt1, paramInt2, true);
        else
            localPagination = this.productDao.getPageByWebsite(paramLong1, null, null, Boolean.valueOf(true), paramBoolean1, paramBoolean2, null, null, null, null, null, null, null, paramInt1, paramInt2, true);
        return localPagination;
    }

    public Pagination getPageByStockWarning(Long paramLong, Integer paramInteger, Boolean paramBoolean, int paramInt1, int paramInt2) {
        return this.productDao.getPageByStockWarning(paramLong, paramInteger, paramBoolean, paramInt1, paramInt2);
    }

    @Transactional(readOnly = true)
    public Pagination getPageForTagChannel(Long paramLong1, Long paramLong2, Long paramLong3, Long paramLong4, Boolean paramBoolean1, String[] paramArrayOfString1, String[] paramArrayOfString2, Boolean paramBoolean2, int paramInt1, int paramInt2, int paramInt3) {
        Pagination localPagination;
        if (paramLong4 != null)
            localPagination = this.productDao.getPageByTag(paramLong4, paramLong3, paramBoolean1, paramBoolean2, paramInt2, paramInt3, true);
        else if (paramLong3 != null)
            localPagination = this.productDao.getPageByCategoryChannel(paramLong1, paramLong3, paramBoolean1, paramArrayOfString1, paramArrayOfString2, paramBoolean2, paramInt1, paramInt2, paramInt3, true);
        else
            localPagination = this.productDao.getPageByWebsite(paramLong2, null, null, Boolean.valueOf(true), paramBoolean1, paramBoolean2, null, null, null, null, null, null, null, paramInt2, paramInt3, true);
        return localPagination;
    }

    public List<Product> getListForTag(Long paramLong1, Long paramLong2, Long paramLong3, Boolean paramBoolean1, Boolean paramBoolean2, Boolean paramBoolean3, Boolean paramBoolean4, int paramInt1, int paramInt2) {
        List localList;
        if (paramLong3 != null)
            localList = this.productDao.getListByTag(paramLong3, paramLong2, paramBoolean1, paramBoolean2, paramInt1, paramInt2, true);
        else if (paramLong2 != null)
            localList = this.productDao.getListByCategory(paramLong2, paramBoolean1, paramBoolean2, paramInt1, paramInt2, true);
        else
            localList = this.productDao.getListByWebsite1(paramLong1, paramBoolean1, paramBoolean2, paramBoolean3, paramBoolean4, paramInt1, paramInt2, true);
        return localList;
    }

    public List<Product> getIsRecommend(Boolean paramBoolean, int paramInt) {
        return this.productDao.getIsRecommend(paramBoolean, paramInt);
    }

    public Integer[] getProductByTag(Long paramLong) {
        return this.productDao.getProductByTag(paramLong);
    }

    public int deleteTagAssociation(ProductTag[] paramArrayOfProductTag) {
        if (ArrayUtils.isEmpty(paramArrayOfProductTag))
            return 0;
        return this.productDao.deleteTagAssociation(paramArrayOfProductTag);
    }

    @Transactional(readOnly = true)
    public Product findById(Long paramLong) {
        Product localProduct = this.productDao.findById(paramLong);
        return localProduct;
    }

    public Product save(Product paramProduct, ProductExt paramProductExt, Long paramLong1, Long paramLong2, Long paramLong3, Long[] paramArrayOfLong, String[] paramArrayOfString1, String[] paramArrayOfString2, String[] paramArrayOfString3, String[] paramArrayOfString4, String[] paramArrayOfString5, String[] paramArrayOfString6, MultipartFile paramMultipartFile) {
        ProductText localProductText = paramProduct.getProductText();
        if (localProductText != null)
            localProductText.setProduct(paramProduct);
        Category localCategory = this.categoryMng.findById(paramLong2);
        if (paramLong3 != null)
            paramProduct.setBrand(this.brandMng.findById(paramLong3));
        Website localWebsite = this.websiteMng.findById(paramLong1);
        paramProduct.setWebsite(localWebsite);
        paramProduct.setConfig(this.shopConfigMng.findById(paramLong1));
        paramProduct.setCategory(localCategory);
        paramProduct.setType(localCategory.getType());
        if (!ArrayUtils.isEmpty(paramArrayOfLong))
            for (Long localLong : paramArrayOfLong)
                paramProduct.addToTags(this.productTagMng.findById(localLong));
        if (!ArrayUtils.isEmpty(paramArrayOfString1))
            paramProduct.setKeywords(Arrays.asList(paramArrayOfString1));
        paramProduct.init();
        this.productDao.save(paramProduct);
        int i;
        if ((paramArrayOfString2 != null) && (paramArrayOfString2.length > 0)) {
            i = 0;
            int len = paramArrayOfString2.length;
            while (i < len) {
                if (!StringUtils.isBlank(paramArrayOfString2[i]))
                    paramProduct.addToExendeds(paramArrayOfString2[i], paramArrayOfString3[i]);
                i++;
            }
        }
        if ((paramArrayOfString4 != null) && (paramArrayOfString4.length > 0)) {
            i = 0;
            int len = paramArrayOfString4.length;
            while (i < len) {
                if (!StringUtils.isBlank(paramArrayOfString4[i]))
                    paramProduct.addToPictures(paramArrayOfString4[i], paramArrayOfString5[i], paramArrayOfString6[i]);
                i++;
            }
        }
        String str = this.realPathResolver.get(localWebsite.getUploadRel());
        _$1(paramProduct, paramProductExt, localCategory.getType(), paramMultipartFile, str);
        this.productExtMng.save(paramProductExt, paramProduct);
        return paramProduct;
    }

    public Product updateByUpdater(Product paramProduct) {
        Updater localUpdater = new Updater(paramProduct);
        Product localProduct = this.productDao.updateByUpdater(localUpdater);
        return localProduct;
    }

    public Product update(Product paramProduct, ProductExt paramProductExt, Long paramLong1, Long paramLong2, Long[] paramArrayOfLong, String[] paramArrayOfString1, String[] paramArrayOfString2, String[] paramArrayOfString3, Map<String, String> paramMap, String[] paramArrayOfString4, String[] paramArrayOfString5, String[] paramArrayOfString6, MultipartFile paramMultipartFile) {
        Product localProduct = findById(paramProduct.getId());
        ProductText localProductText = paramProduct.getProductText();
        if (localProductText != null) {
            Object localObject1 = localProduct.getProductText();
            if (localObject1 != null) {
                ((ProductText) localObject1).setText(localProductText.getText());
                ((ProductText) localObject1).setText1(localProductText.getText1());
                ((ProductText) localObject1).setText2(localProductText.getText2());
            } else {
                localProductText.setId(paramProduct.getId());
                localProductText.setProduct(localProduct);
                localProduct.setProductText(localProductText);
                this.productTextMng.save(localProductText);
            }
        }
        localProduct.removeAllTags();
        Object localObject1 = this.categoryMng.findById(paramLong1);
        localProduct.setCategory((Category) localObject1);
        localProduct.setType(((Category) localObject1).getType());
        if (localProduct.getOnSale() == null)
            localProduct.setOnSale(Boolean.valueOf(false));
        if (paramLong2 != null)
            localProduct.setBrand(this.brandMng.findById(paramLong2));
        else
            localProduct.setBrand(null);
        if (!ArrayUtils.isEmpty(paramArrayOfLong))
            for (Long localLong : paramArrayOfLong)
                localProduct.addToTags(this.productTagMng.findById(localLong));
        if (paramArrayOfString1 != null) {
            List<String> keywords = localProduct.getKeywords();
            keywords.clear();
            keywords.addAll(Arrays.asList(paramArrayOfString1));
        } else {
            localProduct.getKeywords().clear();
        }
        Updater updater = new Updater(paramProduct);
        updater.exclude(Product.PROP_WEBSITE);
        updater.exclude(Product.PROP_PRODUCT_TEXT);
        localProduct = this.productDao.updateByUpdater(updater);
        if (paramMap != null) {
            Map localMap = localProduct.getAttr();
            localMap.clear();
            localMap.putAll(paramMap);
        }
        localProduct.getExendeds().clear();
        int j;
        if ((paramArrayOfString2 != null) && (paramArrayOfString2.length > 0)) {
            j = 0;
            int len = paramArrayOfString2.length;
            while (j < len) {
                if (!StringUtils.isBlank(paramArrayOfString2[j]))
                    localProduct.addToExendeds(paramArrayOfString2[j], paramArrayOfString3[j]);
                j++;
            }
        }
        localProduct.getPictures().clear();
        if ((paramArrayOfString4 != null) && (paramArrayOfString4.length > 0)) {
            j = 0;
            int len = paramArrayOfString4.length;
            while (j < len) {
                if (!StringUtils.isBlank(paramArrayOfString4[j]))
                    localProduct.addToPictures(paramArrayOfString4[j], paramArrayOfString5[j], paramArrayOfString6[j]);
                j++;
            }
        }
        String str = this.realPathResolver.get(localProduct.getWebsite().getUploadRel());
        _$1(localProduct, paramProductExt, ((Category) localObject1).getType(), paramMultipartFile, str);
        this.productExtMng.saveOrUpdate(paramProductExt, localProduct);
        return localProduct;
    }

    public Product update(Product paramProduct) {
        Updater localUpdater = new Updater(paramProduct);
        Product localProduct = this.productDao.updateByUpdater(localUpdater);
        return localProduct;
    }

    public Product[] deleteByIds(Long[] paramArrayOfLong) {
        Product[] arrayOfProduct1 = new Product[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        Object localObject1;
        while (i < j) {
            this.cartItemMng.deleteByProductId(paramArrayOfLong[i]);
            List localList = this.collectMng.findByProductId(paramArrayOfLong[i]);
            localObject1 = localList.iterator();
            while (((Iterator) localObject1).hasNext()) {
                Object localObject2 = (Collect) ((Iterator) localObject1).next();
                this.collectMng.deleteById(((Collect) localObject2).getId());
            }
            localObject1 = this.consultMng.findByProductId(paramArrayOfLong[i]);
            Object localObject2 = ((List) localObject1).iterator();
            while (((Iterator) localObject2).hasNext()) {
                Object localObject3 = (Consult) ((Iterator) localObject2).next();
                this.consultMng.deleteById(((Consult) localObject3).getId());
            }
            localObject2 = this.productStandardMng.findByProductId(paramArrayOfLong[i]);
            Object localObject3 = ((List) localObject2).iterator();
            while (((Iterator) localObject3).hasNext()) {
                ProductStandard localProductStandard = (ProductStandard) ((Iterator) localObject3).next();
                this.productStandardMng.deleteById(localProductStandard.getId());
            }
            localObject3 = findById(paramArrayOfLong[i]);
            ((Product) localObject3).getTags().clear();
            ((Product) localObject3).getFashions().clear();
            ((Product) localObject3).getKeywords().clear();
            ((Product) localObject3).getPopularityGroups().clear();
            arrayOfProduct1[i] = this.productDao.deleteById(paramArrayOfLong[i]);
            i++;
        }
        for (Object t : arrayOfProduct1)
            ((Product) t).removeAllTags();
        return arrayOfProduct1;
    }

    private boolean _$1(Product paramProduct, ProductExt paramProductExt, ProductType paramProductType, MultipartFile paramMultipartFile, String paramString) {
        if ((paramMultipartFile == null) || (paramMultipartFile.isEmpty()))
            return false;
        deleteImage(paramProduct, paramString);
        String str1 = FilenameUtils.getExtension(paramMultipartFile.getOriginalFilename());
        if (!ImageUtils.isImage(str1))
            return false;
        String str2 = FileNameUtils.genPathName();
        File localFile1 = new File(paramString, str2);
        String str3 = "/" + str2 + "/";
        if (!localFile1.exists())
            localFile1.mkdirs();
        String str4 = FileNameUtils.genFileName();
        File localFile2 = new File(localFile1, str4);
        try {
            paramMultipartFile.transferTo(localFile2);
        } catch (Exception localException1) {
            throw new RuntimeException(localException1);
        }
        try {
            String str5 = str4 + Product.DETAIL_SUFFIX + "." + str1;
            File localFile3 = new File(localFile1, str5);
            this.imageScale.resizeFix(localFile2, localFile3, paramProductType.getDetailImgWidth().intValue(), paramProductType.getDetailImgHeight().intValue());
            paramProductExt.setDetailImg(str3 + str5);
            String str6 = str4 + Product.LIST_SUFFIX + "." + str1;
            File localFile4 = new File(localFile1, str6);
            this.imageScale.resizeFix(localFile2, localFile4, paramProductType.getListImgWidth().intValue(), paramProductType.getListImgHeight().intValue());
            paramProductExt.setListImg(str3 + str6);
            String str7 = str4 + Product.MIN_SUFFIX + "." + str1;
            File localFile5 = new File(localFile1, str7);
            this.imageScale.resizeFix(localFile2, localFile5, paramProductType.getMinImgWidth().intValue(), paramProductType.getMinImgHeight().intValue());
            paramProductExt.setMinImg(str3 + str7);
        } catch (Exception localException2) {
            throw new RuntimeException(localException2);
        }
        localFile2.delete();
        return true;
    }

    public void deleteImage(Product paramProduct, String paramString) {
        String str1 = paramProduct.getDetailImg();
        if (!StringUtils.isBlank(str1))
            new File(paramString + str1).delete();
        String str2 = paramProduct.getListImg();
        if (!StringUtils.isBlank(str2))
            new File(paramString + str2).delete();
        String str3 = paramProduct.getMinImg();
        if (!StringUtils.isBlank(str3))
            new File(paramString + str3).delete();
    }

    public Integer getStoreByProductPattern(Long paramLong1, Long paramLong2) {
        ProductFashion localProductFashion = this.productFashionDao.getPfashion(paramLong1, paramLong2);
        return localProductFashion.getStockCount();
    }

    public List<Product> getHistoryProduct(HashSet<Long> paramHashSet, Integer paramInteger) {
        return this.productDao.getHistoryProduct(paramHashSet, paramInteger);
    }

    public Integer getTotalStore(Long paramLong) {
        Product localProduct = this.productDao.findById(paramLong);
        Set localSet = localProduct.getFashions();
        Integer localInteger = Integer.valueOf(0);
        if (localSet != null) {
            Iterator localIterator = localSet.iterator();
            while (localIterator.hasNext()) {
                ProductFashion localProductFashion = (ProductFashion) localIterator.next();
                localInteger = Integer.valueOf(localInteger.intValue() + localProductFashion.getStockCount().intValue());
            }
        }
        return localInteger;
    }
}

